package com.hellobike.haxing.modules.product.service.impl;

import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.product.entity.Spu;
import com.hellobike.haxing.modules.product.entity.SpuAuditLog;
import com.hellobike.haxing.modules.product.mapper.SpuAuditLogMapper;
import com.hellobike.haxing.modules.product.mapper.SpuMapper;
import com.hellobike.haxing.modules.product.service.ISpuAuditLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: spu审核日志
 * @Author: haxing-boot
 * @Date:   2020-02-14
 * @Version: V1.0
 */
@Service
public class SpuAuditLogServiceImpl extends ServiceImpl<SpuAuditLogMapper, SpuAuditLog> implements ISpuAuditLogService {

    @Autowired
    private SpuAuditLogMapper spuAuditLogMapper;
    @Autowired
    private SpuMapper spuMapper;


    @Transactional
    @Override
    public Result<?> auditSpu(SpuAuditLog spuAuditLog) {
        if (spuAuditLog.getAuditStatus() != 3 && spuAuditLog.getAuditStatus() != 4) {
            return Result.error("审核状态不合法");
        }
        Spu spu = spuMapper.selectById(spuAuditLog.getSpuId());
        if (spu.getStatus() != 2) {
            return Result.error("当前SPU非待审核状态");
        }
        //记录审核日志
        spuAuditLogMapper.insert(spuAuditLog);
        //更新spu状态
        Spu updateModel = new Spu();
        updateModel.setId(spuAuditLog.getSpuId());
        updateModel.setStatus(spuAuditLog.getAuditStatus());
        spuMapper.updateById(updateModel);
        return Result.ok();
    }

}
